//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BicubicSpline.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Math/Interpolations/Interpolation2D.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CBicubicSpline::CBicubicSpline (boost::shared_ptr<QuantLib::BicubicSpline>& childNative, Object^ owner) : CInterpolation2D(CBicubicSpline::typeid)
{
#ifdef HANDLE
	_phBicubicSpline = NULL;
#endif
	_ppBicubicSpline = &childNative;
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppBicubicSpline));
}
Cephei::QL::Math::Interpolations::CBicubicSpline::CBicubicSpline (QuantLib::BicubicSpline& childNative, Object^ owner) : CInterpolation2D(CBicubicSpline::typeid)
{
#ifdef HANDLE
	_phBicubicSpline = NULL;
#endif
	_ppBicubicSpline = new boost::shared_ptr<QuantLib::BicubicSpline> (&childNative);
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppBicubicSpline));
    _BicubicSplineOwner = owner;
    _Interpolation2DOwner = owner;
}

Cephei::QL::Math::Interpolations::CBicubicSpline::CBicubicSpline (CBicubicSpline^ copy) : CInterpolation2D(CBicubicSpline::typeid)
{
#ifdef HANDLE
	_phBicubicSpline = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBicubicSpline = new boost::shared_ptr<QuantLib::BicubicSpline> (copy->GetShared());
        _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppBicubicSpline));
    }
}
Cephei::QL::Math::Interpolations::CBicubicSpline::CBicubicSpline (System::Type^ t) : CInterpolation2D(CBicubicSpline::typeid)
{
#ifdef HANDLE
	_phBicubicSpline = NULL;
#endif
	if (!t->IsSubclassOf(CBicubicSpline::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CBicubicSpline::CBicubicSpline (QuantLib::Handle<QuantLib::BicubicSpline>& childNative, Object^ owner)  : CInterpolation2D(CBicubicSpline::typeid)
{
	_phBicubicSpline = &childNative;
	_ppBicubicSpline = &static_cast<boost::shared_ptr<QuantLib::BicubicSpline>>(childNative.currentLink());
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppBicubicSpline));
    _BicubicSplineOwner = owner;
}
Cephei::QL::Math::Interpolations::CBicubicSpline::CBicubicSpline (QuantLib::Handle<QuantLib::BicubicSpline> childNative)  : CInterpolation2D(CBicubicSpline::typeid)
{
	_phBicubicSpline = &childNative;
	_ppBicubicSpline = &static_cast<boost::shared_ptr<QuantLib::BicubicSpline>>(childNative.currentLink());
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppBicubicSpline));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CBicubicSpline::CBicubicSpline (QuantLib::BicubicSpline childNative)  : CInterpolation2D(CBicubicSpline::typeid)
{
#ifdef HANDLE
	_phBicubicSpline = NULL;
#endif
	_ppBicubicSpline = new boost::shared_ptr<QuantLib::BicubicSpline> (new QuantLib::BicubicSpline (childNative));
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppBicubicSpline));
}
#endif

Cephei::QL::Math::Interpolations::CBicubicSpline::~CBicubicSpline ()
{
    if (_ppBicubicSpline != NULL)
    {
	    delete _ppBicubicSpline;
        _ppBicubicSpline = NULL;
    }
}
Cephei::QL::Math::Interpolations::CBicubicSpline::!CBicubicSpline ()
{
    if (_ppBicubicSpline != NULL)
    {
	    delete _ppBicubicSpline;
    }
}
QuantLib::BicubicSpline& Cephei::QL::Math::Interpolations::CBicubicSpline::GetReference ()
{
    if (_ppBicubicSpline == NULL) throw gcnew NativeNullException ();
	return **_ppBicubicSpline;
}
boost::shared_ptr<QuantLib::BicubicSpline>& Cephei::QL::Math::Interpolations::CBicubicSpline::GetShared ()
{
    if (_ppBicubicSpline == NULL) throw gcnew NativeNullException ();
	return *_ppBicubicSpline;
}
QuantLib::BicubicSpline* Cephei::QL::Math::Interpolations::CBicubicSpline::GetPointer ()
{
    if (_ppBicubicSpline == NULL) throw gcnew NativeNullException ();
	return &**_ppBicubicSpline;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BicubicSpline>& Cephei::QL::Math::Interpolations::CBicubicSpline::GetHandle ()
{
	if (_phBicubicSpline == NULL)
	{
		_phBicubicSpline = new Handle<QuantLib::BicubicSpline> (*_ppBicubicSpline);
	}
	return *_phBicubicSpline;
}
#endif
bool Cephei::QL::Math::Interpolations::CBicubicSpline::HasNative () 
{
	return (_ppBicubicSpline != NULL);
}

Double Cephei::QL::Math::Interpolations::CBicubicSpline::DerivativeX (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBicubicSpline)->derivativeX ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CBicubicSpline::DerivativeXY (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBicubicSpline)->derivativeXY ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CBicubicSpline::DerivativeY (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBicubicSpline)->derivativeY ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CBicubicSpline::SecondDerivativeX (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBicubicSpline)->secondDerivativeX ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CBicubicSpline::SecondDerivativeY (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBicubicSpline)->secondDerivativeY ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

